#!/usr/bin/env python 

import pygame, os, sys, time
from pygame.locals import *

# colours
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
PITCH = (0, 128, 0)
		
import player, gkeeper, pointer, ball
	
def playgame():
	pygame.init()
		
	# Setting up environment
	WINDOWHEIGHT = 800
	WINDOWWIDTH = 600
	font = pygame.font.Font(None, 36)
	screen = pygame.display.set_mode((WINDOWHEIGHT,WINDOWWIDTH), 0, 32)
	pygame.display.set_caption("Penalty Shootout Game")
	background = pygame.Surface(screen.get_size())
	background = pygame.image.load("data/img/background.png").convert()
	background = background.convert()
	#background.fill(PITCH)
	clock = pygame.time.Clock()
	playgame.score = 0
	playgame.scoremultiplier = 1.0
	playgame.level = 1
	
	# Loading these here for speed 
	playgame.saveimg = pygame.image.load("data/img/saved.png").convert()
	playgame.scoredimg = pygame.image.load("data/img/scored.png").convert()
	
	# Instantiating game objects
	theplayer = player.Player()
	thepointer = pointer.Pointer()
	thekeeper = gkeeper.Gkeeper()
	theball = ball.Ball()
	
	sprites = pygame.sprite.RenderPlain(thepointer, thekeeper, theball)
	
	def reset():
		for o in sprites:
			o.reset()
	
	def shot():
		# Function to handle everything that happens during a shot ( if you include save() and goal() )
		thepointer.stopandstart()
		theplayer.kicking()
		screen.blit(background, (0,0))
		screen.blit(theplayer.image, (theplayer.x, theplayer.y))

		theball.rect.x = thepointer.rect.x - 10
		theball.rect.y = thepointer.rect.y - 10

		if thepointer.rect.x < ((pointer.Goal.topleftx + pointer.Goal.width) /3):
			thekeeper.diver(1)
		elif thepointer.rect.x > ((pointer.Goal.topleftx + pointer.Goal.width) /3)*2:
			thekeeper.diver(2)
		else:
			thekeeper.diver(3)
		
		sprites.draw(screen)
		pygame.display.flip()
		
		pygame.time.wait(800)
		
		if pygame.sprite.collide_mask(theball, thekeeper) or (not thepointer.inrangex()) \
		or (not thepointer.inrangey()):
			playgame.score -= save()
		else:
			playgame.score += goal()
			scored = True
	
	def save():
		#screen.fill(GREEN)
		screen.blit(playgame.saveimg, (0,0))
		pygame.display.flip()
		tscore = (10 * playgame.scoremultiplier)
		
		pygame.time.wait(2000)
		
		reset()
		theplayer.rest()
		return tscore
		
	def goal():
		#screen.fill(RED)
		screen.blit(playgame.scoredimg, (0,0))
		pygame.display.flip()
		tscore = (10 * playgame.scoremultiplier)
		
		pygame.time.wait(2000)
		
		playgame.scoremultiplier += 1
		playgame.level += 1
		thepointer.speedlimit += 0.2
		reset()
		theplayer.rest()
		return tscore
	
	
	# Game Loop
	while True:
		clock.tick(100)
		
		for event in pygame.event.get():
			if event.type == QUIT:
				return
			if (event.type == KEYDOWN):
				if (event.key == K_q):
					return playgame.score
					
				if (event.key == K_ESCAPE):
					return playgame.score	
					
				if (event.key == K_SPACE):
					shot()
					
				if (event.key == K_r):
					reset()
		
		sprites.update()
		
		# Draw score
		scorestring = "Score: " + str(playgame.score)
		clearrect = Rect(0,0,150,30)
		pygame.draw.rect(background, PITCH, clearrect)
		scoretext = font.render(scorestring, 1, RED)
		scorepos = (0,0)
		background.blit(scoretext, scorepos)
		
		# Draw level indicator
		levelstring = "Level " + str(playgame.level)
		clearrect = Rect(700,0,100,30)
		pygame.draw.rect(background, PITCH, clearrect)
		leveltext = font.render(levelstring, 1, RED)
		levelpos = (700,0)
		background.blit(leveltext, levelpos)
		
		# Draw hints
		hintstring1 = "r - reset"
		hintstring2 = "space - shoot"
		hinttext1 = font.render(hintstring1, 1, RED)
		hinttext2 = font.render(hintstring2, 1, RED)
		hintpos1 = (0,580)
		hintpos2 = (0,550)
		background.blit(hinttext1, hintpos1)
		background.blit(hinttext2, hintpos2)
		
		# Draw images
		screen.blit(background, (0,0))
		screen.blit(theplayer.image, (theplayer.x, theplayer.y))
		sprites.draw(screen)
		
		# Refresh the screen
		pygame.display.flip()
		
		

if __name__ == "__main__":
	playgame()
